package cn.qihang.ssm.service.impl;

import cn.qihang.ssm.mapper.UserMapper;
import cn.qihang.ssm.pojo.Role;
import cn.qihang.ssm.pojo.UserInfo;
import cn.qihang.ssm.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: qihang
 * @Date: 2022/11/2 15:53
 * @Desc:
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    /**
     * 认证--查询用户
     * @param s
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        UserInfo userInfo = this.userMapper.findUserByUserName(s);
        //把查询到的用户名 密码 封装到安全框架的user对象中
        User user = new User(
                userInfo.getUsername(),
                userInfo.getPassword(),
                userInfo.getStatus() == 0 ? false : true ,//用户是否开启 0 表示未开启，1表示开启
                true,  //帐号是否过期  不过期
                true, //证书  不过期
                true,// 帐号 不锁定
                getAuthority(userInfo.getRoles()));

        return user;
    }

    /**
     * 权限认证方法
     * @return
     */
    private List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {
        List<SimpleGrantedAuthority> list = new ArrayList<>();
        for (Role role : roles) {
            list.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
        }
        return  list;
    }


    /**
     * 查询所有用户
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<UserInfo> findAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return this.userMapper.findAll();
    }

    /**
     * 新增用户
     * @param userInfo
     */
    @Override
    public void save(UserInfo userInfo) {
        //进行加密
        userInfo.setPassword(bCryptPasswordEncoder.encode(userInfo.getPassword()));
        this.userMapper.save(userInfo);
    }


    /**
     * 用户详情
     * @param id
     * @return
     */
    @Override
    public UserInfo findById(Integer id) {
       return this.userMapper.findById(id);
    }


    /**
     * 添加用户前，查询添加的用户是谁
     * @param id
     * @return
     */
    @Override
    public UserInfo findUserById(Integer id) {
        return this.userMapper.findUserById(id);
    }


    /**
     * 根据用户查询哪些角色是可以添加（除了本身以经有的角色以外的才可以添加）
     * @param id
     * @return
     */
    @Override
    public List<Role> findOtherRoles(Integer id) {
        return this.userMapper.findOtherRoles(id);
    }


    /**
     * 给用户添加角色
     * @param userId
     * @param ids
     */
    @Override
    public void addRoleToUser(Integer userId, Integer[] ids) {

        for (Integer roleId : ids) {
            this.userMapper.addRoleToUser(userId,roleId);
        }
    }


    /**
     * 删除用户
     * @param id
     */
    @Override
    public void deleteUser(Integer id) {
        this.userMapper.deleteUser(id);
    }

}
